<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/14
 * Time: 14:10
 */

namespace App\Http\Controllers\HumanResourceManage;


use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Model\EmployeeModel;
use App\Http\Model\GroupMembersModel;
use App\Http\Model\GroupModel;
use App\Http\Model\TechnicalCompanyModel;
use App\Http\Model\TechnicalGroupModel;
use App\Http\Model\WorkmanAccountsModel;
use App\Http\Model\WorkmanAssignmentModel;
use App\Http\Model\YearModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class TechnicalController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function companyLists(Request $request)
    {
        $rules = [
            'length' => 'required|integer|in:10,20,50',
            'page' => 'required|integer|min:1',
        ];
        $message = [
            'length.required' => '获取记录条数失败',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.required' => '获取起始记录位置失败',
            'page.integer' => '页码参数类型错误',
            'page.min' => '页码参数值不小于:min',
        ];
        $param = $request->only('length','page','search');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $technicalCompanyModel = new TechnicalCompanyModel();
            $lists = $technicalCompanyModel->lists($param);
            $countLists = $technicalCompanyModel->countLists($param);
            $this->data = $this->tableFormat($lists,$countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 450101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function addCompany(Request $request)
    {
        $rules = [
            'chargePerson' => 'required',
            'name' => 'required',
            'order' => 'integer|min:1',
        ];
        $message = [
            'chargePerson.required' => '请填写负责人',
            'name.required' => '请填写施工队名称',
            'order.integer' => '排序参数类型错误',
            'order.min' => '排序参数值不小于:min',
        ];
        $param = $request->only(['chargePerson','order','name']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $technicalCompanyModel = new TechnicalCompanyModel();
            $technicalCompanyModel->insert($param);
        } else {
            throw new ApiException($validator->errors()->first(), 450201);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function editCompany(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'chargePerson' => 'required',
            'name' => 'required',
            'order' => 'integer|min:1',
        ];
        $message = [
            'id.required' => '专业施工队伍参数类型错误',
            'id.integer' => '专业施工队伍参数类型错误',
            'name.required' => '请填写施工队名称',
            'chargePerson.required' => '请填写负责人',
            'order.integer' => '排序参数类型错误',
            'order.min' => '排序参数值不小于:min',
        ];
        $param = $request->only(['id','chargePerson','order','name']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $technicalCompanyModel = new TechnicalCompanyModel();
            $id = $param['id'];
            unset($param['id']);
            $technicalCompanyModel->update(['id' => $id], $param);
        } else {
            throw new ApiException($validator->errors()->first(), 450301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function deleteCompany(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '专业施工队伍参数类型错误',
            'id.integer' => '专业施工队伍参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $technicalCompanyModel = new TechnicalCompanyModel();
            $technicalGroupModel = new TechnicalGroupModel();
            $info = $technicalGroupModel->info(['companyId'=>$param['id']]);
            if (!empty($info)) {
                throw new ApiException('该专业施工班组下还有施工队伍，不能删除', 450402);
            }
            $technicalCompanyModel->delete(['id' => $param['id']]);
        } else {
            throw new ApiException($validator->errors()->first(), 450401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function companyInfo(Request $request){
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '专业施工队伍参数类型错误',
            'id.integer' => '专业施工队伍参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $technicalCompanyModel = new TechnicalCompanyModel();
            $this->data = $technicalCompanyModel->info($param);
        } else {
            throw new ApiException($validator->errors()->first(), 450501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function groupLists(Request $request)
    {
        $rules = [
            'companyId' => 'required|integer',
            'professionId' => 'nullable|integer',
            'length' => 'required|integer|in:10,20,50',
            'page' => 'required|integer|min:0',
        ];
        $message = [
            'companyId.required' => '获取专业施工班组参数失败',
            'companyId.integer' => '专业施工班组参数类型错误',
            'professionId.integer' => '工种参数类型错误',
            'length.required' => '获取记录条数失败',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.required' => '获取起始记录位置失败',
            'page.integer' => '页码参数类型错误',
            'page.min' => '页码参数值不小于:min',
        ];
        $param = $request->only('companyId','professionId','length','page','search');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $technicalGroupModel = new TechnicalGroupModel();
            $lists = $technicalGroupModel->lists($param);
            $countLists = $technicalGroupModel->countLists($param);
            $this->data = $this->tableFormat($lists,$countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 450601);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function addGroup(Request $request){
        $rules = [
            'companyId' => 'required|integer',
            'chargePerson' => 'required',
            'professionId' => 'required|integer',
            'idcard'=>'required|size:18',
            'order'=>'nullable|integer'
        ];
        $message = [
            'companyId.required' => '获取专业施工班组参数失败',
            'companyId.integer' => '专业施工班组参数类型错误',
            'chargePerson.required' => '请填写负责人',
            'idcard.required' => '请填写身份证号',
            'idcard.size' => '身份证号位数不正确',
            'professionId.required' => '请选择工种',
            'professionId.integer' => '工种参数类型错误',
            'order.integer' => '排序参数类型错误',
        ];
        $param = $request->only(['companyId','chargePerson','professionId','phone','idcard','order']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $technicalGroupModel = new TechnicalGroupModel();
            $param['createTime'] = date('Y-m-d H:i:s');
            $technicalGroupModel->insert($param);
        } else {
            throw new ApiException($validator->errors()->first(), 450701);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function editGroup(Request $request){
        $rules = [
            'id' => 'required|integer',
            'chargePerson' => 'required',
            'professionId' => 'required|integer',
            'idcard'=>'required|size:18',
            'order'=>'nullable|integer'
        ];
        $message = [
            'id.required' => '获取专业施工班组参数失败',
            'id.integer' => '专业施工班组参数类型错误',
            'chargePerson.required' => '请填写负责人',
            'idcard.required' => '请填写身份证号',
            'idcard.size' => '身份证号位数不正确',
            'professionId.required' => '请选择工种',
            'professionId.integer' => '工种参数类型错误',
            'order.integer' => '排序参数类型错误',
        ];
        $param = $request->only(['id','chargePerson','professionId','phone','idcard','order']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $technicalGroupModel = new TechnicalGroupModel();
            $id = $param['id'];
            unset($param['id']);
            $technicalGroupModel->update(['id'=>$id],$param);
        } else {
            throw new ApiException($validator->errors()->first(), 450801);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function groupInfo(Request $request){
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '专业施工班组参数类型错误',
            'id.integer' => '专业施工班组参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $technicalGroupModel = new TechnicalGroupModel();
            $this->data = $technicalGroupModel->info($param);
        } else {
            throw new ApiException($validator->errors()->first(), 450901);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function deleteGroup(Request $request){
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '专业施工队伍参数类型错误',
            'id.integer' => '专业施工队伍参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $technicalGroupModel = new TechnicalGroupModel();
            $info = $technicalGroupModel->info(['id'=>$param['id']]);
            if ($info['projectId'] && $info['groupId']){
                $workmanAssignmentModel = new WorkmanAssignmentModel();
                $workmanAccountsModel = new WorkmanAccountsModel();
                $assignment = $workmanAssignmentModel->info(['groupId'=>$info['groupId']]);
                $separate = $workmanAccountsModel->info(['groupId'=>$info['groupId']]);
                if ($assignment || $separate) {
                    throw new ApiException('该施工队伍下已有分账不能删除', 451002);
                }
            }
            $technicalGroupModel->delete(['id' => $param['id']]);
        } else {
            throw new ApiException($validator->errors()->first(), 451001);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 专业班组调配项目
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function adjustProject(Request $request){
//        $rules = [
//            'projectId' => 'required|integer',
//            'jobNumber' => 'required',
//            'id' => 'required|integer',
//        ];
//        $message = [
//            'projectId.required' => '请选择项目',
//            'projectId.integer' => '项目参数类型错误',
//            'id.required' => '专业施工队伍参数获取失败',
//            'id.integer' => '专业施工队伍参数类型错误',
//        ];
//        $param = $request->only(['projectId','id','jobNumber']);
//        $validator = Validator::make($param, $rules, $message);
//        if ($validator->passes()) {
//            $employeeModel = new EmployeeModel();
//            //如果专业班组两年都在同一项目  employee中会出现两条technicalGroupId相同的记录
//            $yearModel = new YearModel();
//            $yearInfo = $yearModel->findYear(date('Y-m-d H:i:s'));
//            $employeeInfo = $employeeModel->info(['technicalGroupId'=>$param['id'],'startTime'=>$yearInfo['startTime'],'endTime'=>$yearInfo['endTime']]);
//            if(!empty($employeeInfo)){
//                $employeeModel->update(['technicalGroupId'=>$param['id']],['projectId'=>$param['projectId']]);
//                $groupMembersModel = new GroupMembersModel();
//                $groupMemberInfo = $groupMembersModel->info(['employeeId'=>$employeeInfo['id']]);
//                if(!empty($groupMemberInfo)){
//                    $groupModel = new GroupModel();
//                    $groupModel->update(['id'=>$groupMemberInfo['groupId']],['projectId'=>$param['projectId']]);
//                }
//            }else{
//                $technicalGroupModel = new TechnicalGroupModel();
//                $info = $technicalGroupModel->info(['id'=>$param['id']]);
//                $data = [
//                    'name'=>$info['chargePerson'],
//                    'idcard'=>$info['idcard'],
//                    'jobNumber'=>$param['jobNumber'],
//                    'phone'=>$info['phone'],
//                    'professionId'=>$info['professionId'],
//                    'projectId'=>$param['projectId'],
//                    'entryTime'=>date('Y-m-d H:i:s'),
//                    'technicalGroupId'=>$param['id'],
//                ];
//                $employeeModel->insert($data);
//            }
//        } else {
//            throw new ApiException($validator->errors()->first(), 451101);
//        }
//        return $this->ajaxResult($this->code, $this->msg, $this->data);

        $rules = [
            'employeeId' => 'nullable|integer',
            'groupId' => 'nullable|integer',
            'id' => 'required|integer',
        ];
        $message = [
            'employeeId.integer' => '工人参数类型错误',
            'groupId.integer' => '班组参数类型错误',
            'id.required' => '专业施工队伍参数获取失败',
            'id.integer' => '专业施工队伍参数类型错误',
        ];
        $param = $request->only(['employeeId','id','groupId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $technicalGroup = DB::table('technical_group')->where('id',$param['id'])->first();
            if (!empty($param['employeeId'])){
                $employee = DB::table('employee')->where('id',$param['employeeId'])->first();
                if (!is_null($employee)){
                    $data['employeeId'] = $employee->id;
                }
            }
            if (!empty($param['groupId'])){
                $group = DB::table('group')->where('id',$param['groupId'])->first();
                if (!is_null($group)){
                    $data['groupId'] = $group->id;
                }
            }
            if (!empty($data)){
                DB::table('technical_group')->where('id',$param['id'])->update($data);
            }
        } else {
            throw new ApiException($validator->errors()->first(), 451101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }
}